//
//  NSString+util.m
//  LaomiKit
//
//  Created by laomi on 2020/7/16.
//  Copyright © 2020 laomi. All rights reserved.
//

#import "NSString+util.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSString (util)

-(BOOL)laomi_checkIsPhoneNumber
{
    NSString *phoneRegex = @"^(1[3-9])\\d{9}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:self];
}
-(BOOL)laomi_checkIsvalidateEmail

{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    
    return [emailTest evaluateWithObject:self];
}

+(NSString*)MD5ForString:(NSString*)oldStr;
{
    if (!oldStr) {
        return nil;
    }
    const char *cStr = [oldStr UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    
    CC_MD5(cStr, (CC_LONG)strlen(cStr), digest);
    
    NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [result appendFormat:@"%02X", digest[i]];
    }
    return result;
}
@end
